import json
from typing import List

from openai.types.beta import Assistant
from llm.models import Assistants
from users.models import Users


class AssistantsDAO:
    @classmethod
    def add(cls, assistant: Assistant, user: Users) -> Assistants:
        """
        新增一个新的assistant到数据库中注册，注意：这里传入的assistant是openai.types.beta.Assistant类型，
        :return: Assistants(数据库)
        """
        try:
            return Assistants.objects.create(
                assistant_name=assistant.name,
                assistant_id=assistant.id,
                information=assistant.metadata.__str__(),
                user_id=user,
            )
        except Exception as e:
            print(f"Error in AssistantsDAO.add: {e}")
            raise e

    @classmethod
    def get_all(cls, user: Users) -> List[Assistants]:
        try:
            return Assistants.objects.filter(user_id=user)
        except Exception as e:
            print(f"Error in AssistantsDAO.get: {e}")
            raise e
